﻿@{
    ViewBag.Title = "newindex";
}
@section scripts
{  
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js?ver=GetDate()")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/common/export.js")" type="text/javascript"></script> }
   <div class="mini-splitter" style="width:100%;height:100%;">
    <div size="270" showcollapseButton="true">
        <div class="mini-toolbar" style="padding:5px;border-top:6px;border-left:0;border-right:0;">                
            <a class="mini-button" onclick="LoadListTree" plain="true">刷新</a> 
            <a class="mini-button" id="cnew" name="cnew" onclick="addClass" plain="true" visible="@ViewBag.cadd">新增分类</a>
            <a class="mini-button" id="cedit" name="cedit" onclick="editClass" plain="true" visible="@ViewBag.cedit">修改分类</a> 
            <a class="mini-button" id="cdelcls" name="cdelcls" onclick="removeClass" plain="true" visible="@ViewBag.cremove">删除分类</a>                            
        </div>
        <div class="mini-toolbar" style="padding:0px;border-top:0px;border-left:0;border-right:0;"> 
            <table>
                <tr>
                    <td width ="60px" align="right">分类名称</td>
                    <td width ="120px" ><input class="mini-textbox" id ="TreeKey" style="width:120px;left:3px;" name = "TreeKey" onkeypress="setkeyPressCls()"/></td>
                    <td width ="80px" align="center">
                        <a class="mini-button" iconCls="icon-search" onclick="findtree" plain="true">搜索</a>
                    </td>
                </tr>
                <tr>
                    <td width ="60px" align="right">显示分店</td>                    
                    <td width ="200px" align="center" colspan="2" >
                        @Html.Partial("~/Views/Modular/LookupShopInfo.cshtml")
                    </td>
                </tr>
            </table>
        </div>

        <div class="mini-fit">
           <ul id="tree1" class="mini-tree" style="width:100%;height:100%;" expandOnLoad="0"
              textField="FName" idField="FItemID" parentField="FParentID" showTreeIcon="true" resultAsTree="false">        
           </ul>
        </div>
    </div>
       
    <div showCollapseButton="true">
        <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
            <table style="width:100%;">
                <tr>
                    <td style="width:100%;">
                        <a class="mini-button" id="fresh" name="fresh" iconCls="icon-search" onclick="search(0)" plain="true">查询</a>
                        <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="newGoods" plain="true" visible="@ViewBag.add">新增商品</a>
                        <a class="mini-button" id="save" name="save" iconCls="icon-save" onclick="saveas" plain="true" visible="@ViewBag.save">保存</a>
                        <a class="mini-button" id="edit" name="edit" iconCls="icon-edit" onclick="modify" plain="true" visible="@ViewBag.edit">修改</a>
                        <a class="mini-button" id="delrow" name="delrow" iconCls="icon-remove" onclick="removeRows" plain="true" visible="@ViewBag.remove">删除</a>                 
                        <a class="mini-menubutton" plain="true" iconCls="icon-filter" menu="#popupBatch">选项</a>
                        <a class="mini-menubutton" plain="true" iconCls="icon-expand" menu="#popupMenu">查看</a>
                        @*<a class="mini-menubutton" plain="true" iconCls="icon-download" menu="#popupMenuFile">文件</a>*@
                         <span class="separator"></span>
                        <a class="mini-button" id="close" name="close" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                    </td>
                </tr>
            </table>       
        </div>
        <table>
            <tr>
                <td width ="70px" height="29px" align="right">编码/条码</td>
                <td width ="190px" ><input class="mini-textbox" width ="190px" id ="FSearchKey" name = "FSearchKey" onkeypress="setkeyPress()"/>
                </td>
                <td width ="70px" height="29px" align="right">商品名称</td>
                <td width ="190px" ><input class="mini-textbox" width ="190px" id ="FSearchName" name = "FSearchName" onkeypress="setkeyPress()"/>
                </td>
            </tr>
        </table>
        <div class="mini-fit" >
            <div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;" pageSize="15"
                    borderStyle="border:1;" url="../Goods/GetGoodsList"
                    allowCellSelect="true" allowCellEdit="true" showColumnsMenu="true" fitColumns="false"  
                    onrowdblclick="modify"  sortMode="client"multiSelect="true" onload="loaded">
                <div property="columns">
                    <div type="checkcolumn"></div>
                    <div type="indexcolumn" width="40" headerAlign="center">序号</div>
                    <div field="FItemID" name="FItemID" width="0" headeralign="center" allowsort="true">ID号</div>                    
                    <div field="FCode" name="FCode" width="110" headeralign="center" allowsort="true">商品条码</div>
                    <div field="FName" name="FName" width="150" headeralign="center" allowsort="true">商品名称</div>
                    <div field="FSpec" name="FSpec" width="90" headeralign="center" allowsort="true">规格型号</div>
                    <div field="FUnitName" name="FUnitName" width="40" headeralign="center" align="center" allowsort="true">单位</div>
                    <div field="FPurPrice" name="FPurPrice" width="70" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">进货价</div>
                    <div field="FPosPrice" name="FPosPrice" width="70" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">零售价</div>
                    <div field="FRmoPrice" name="FRmoPrice" width="70" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">配送价</div>
                    <div field="FSelPrice" name="FSelPrice" width="70" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">批发价</div>
                    <div field="FVipPrice" name="FVipPrice" width="70" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">会员价</div>
                    <div field="FMallPrice" name="FMallPrice" width="70" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">电商价</div>
                    <div field="FRequireFlag" type="checkboxcolumn" name="FRequireFlag" trueValue="1" falseValue="0" width="60" headeralign="center" allowsort="true">要货申请</div>
                    <div field="FRequireStop" type="checkboxcolumn" name="FRequireStop" trueValue="1" falseValue="0" width="60" headeralign="center" allowsort="true">请货售罄</div>
                    <div field="FMallFlag" type="checkboxcolumn" name="FMallFlag" trueValue="1" falseValue="0" width="60" headeralign="center" allowsort="true">电商上架</div>
                    <div field="FHotSale" type="checkboxcolumn" name="FHotSale" trueValue="1" falseValue="0" width="60" headeralign="center" allowsort="true">电商热销</div>
                    <div field="FMallPoint" name="FMallPoint" width="70" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">积分兑换</div>
                    <div field="FWeighPlu" name="FWeighPlu" width="60" headeralign="center" align="center" allowsort="true">称重PLU</div>
                    <div field="FConvert" name="FConvert" width="70" headeralign="center" allowsort="true">内装数</div>     
                    <div field="FNumber" name="FNumber" width="100" headeralign="center" allowsort="true">商品编码</div>               
                    <div field="FClassName" name="FClassName" width="120" headeralign="center" allowsort="true">分类名称</div>
                    <div field="FCorpName" name="FCorpName" width="150" headeralign="center" allowsort="true">所属商户</div>
                    <div field="FPriceBookName" name="FPriceBookName" width="150" headeralign="center" allowsort="true">组织机构</div>
                    <div field="FAttribute" name="FAttribute" width="90" headeralign="center" allowsort="true">商品属性</div>
                    <div field="FMallUnit" name="FMallUnit" width="90" headeralign="center" allowsort="true">电商单位</div>
                    <div field="FMallConvert" name="FMallConvert" width="80" headeralign="center" allowsort="true">电商内装数</div>
                    <div field="FLowLimit" name="FLowLimit" width="70" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">库存下限</div>
                    <div field="FHightLimit" name="FHightLimit" width="70" numberFormat="#,0.00" headerAlign="center" align="right" allowSort="true">库存上限</div>                    
                    <div field="FModifyTime" name="FModifyTime" width="170" headeralign="center" allowsort="true" dateFormat="yyyy-MM-dd HH:mm:ss">修改日期</div>
                    <div field="FGoodImg" name="FGoodImg" width="0" headeralign="center" allowsort="true" dateFormat="">图片URL</div>
                </div>
            </div>
        </div>
    </div>        
</div>

    <!--menu-->
    <ul id="popupMenu" class="mini-menu" style="display:none;">
        <li id="import" name="import" iconCls="icon-find" onclick="ExcelImport" visible="@ViewBag.import">从Excel导入</li>
        @*<li id="import" name="copyfrom" iconCls="icon-collapse" onclick="CopyFrom" visible="@ViewBag.import">从商品库导入</li>*@
        <li id ="export" name="export" iconCls="icon-filter" onclick="setExportExcel('datagrid1')" visible="@ViewBag.export">导出到Excel</li>
        <li class="separator"></li>
        @*<li id="getLocaData0" iconCls="icon-search" onclick="search(0)">刷新</li>*@
	    <li id="getLocaData1" iconCls="icon-undo" onclick="search(1)">已上图商品</li>
        <li id="getLocaData2" iconCls="icon-edit" onclick="search(2)">未上图商品</li>
        <li id="getLocaData3" iconCls="icon-unlock" onclick="search(3)">已上架商品</li>
        <li id ="getLocaData4"iconCls="icon-redo" onclick="search(4)">未上架商品</li>
        <li id ="getLocaData5"iconCls="icon-find" onclick="search(5)">无库存商品</li>
        <li id ="getLocaData6"iconCls="icon-new" onclick="search(6)">生鲜商品</li>
    </ul>

    <ul id="popupMenuFile" class="mini-menu" style="display:none;">

        <!--<li class="separator"></li>
        <li id ="getFromLoca"iconCls="icon-edit" onclick="ImportGoods">从商品库引入</li>-->
    </ul>

    <ul id="popupBatch" class="mini-menu" style="display:none;">
        <li id="Batchset" name="Batchset" iconCls="icon-find" onclick="Batchset" visible="@ViewBag.batchset">批量设置</li>
        <!--<li id ="setMallFlag" iconCls="icon-edit" onclick="setMallFlag()" visible="@ViewBag.batchset">所选商品全部【上架】</li>
        <li id ="setRequire"iconCls="icon-unlock" onclick="setRequire()" visible="@ViewBag.batchset">所选商品全部【要货】</li>-->
        <li class="separator"></li>        
        <li id ="setMallPrice"iconCls="icon-new" onclick="setMallPrice()" visible="@ViewBag.batchset">同步总店电商价</li>
    </ul>

<script>

    mini.parse();
    var tree = mini.get("tree1");
    var grid = mini.get("datagrid1");//==

    //通用shown自动调用些方式
    function FormShown() {        
        LoadListTree(0);
        mini.getbyName("export").set({ visible: false });
        tree.on("nodeselect", function (e) {
            LoadListData(0);
        });
        LoadListData(0);
        InitGrid('GoodsList');
   }

    function ss() {
        grid.gotoPage(3, 20);
    }

    function setkeyPress() {
        if (event.keyCode == 13) {
            search(0);
        }
    }

    grid.on("drawcell", function (e) {
        //var record = e.record;
        //if (record.FModifyFlag != 1) {
        //    e.cellStyle = "color:#007ACC;";
        //}
    });

    //加载分类树
    function LoadListTree(c) {
        c = c || 0;
        var mShopID = mini.getbyName("FShopID").getValue();
        var sSystemAppID = parent.window.getSystemAppID();
        var json = '[{"mSystemAppID":"' + sSystemAppID + '","mShopID":"' + mShopID + '"}]'
        $.post('GetGoodsClassTree', { mjson: json }, function (data) {
            tree.loadList(mini.decode(data));
            tree.select(0);
            if (c == 1)
                tree.collapseAll();
        });
    }

    function reLoadListTree() {
        var mShopID = mini.getbyName("FShopID").getValue();
        var sSystemAppID = parent.window.getSystemAppID();
        var node = tree.getSelectedNode();
        var json = '[{"mSystemAppID":"' + sSystemAppID + '","mShopID":"' + mShopID + '"}]'
        
        $.post('GetGoodsClassTree', { mjson: json }, function (data) {
            tree.loadList(mini.decode(data));
            tree.selectNode(node);
        });
    }

    //加载资料列表
    function LoadListData(mdatatype) {
        var sSystemAppID = parent.window.getSystemAppID();        
        var mload = tree.getData();
        if (mload.length > 0) {
            var node = tree.getSelectedNode();
            var classid = node.FItemID;
            var json = "[{";
            json = json + "'FSystemAppID':" + "'" + sSystemAppID + "',";
            json = json + "'mClassID':" + "'" + classid + "',";
            json = json + "'mSubCorpID':" + "'" + node.FCorpID + "',";            
            json = json + "'mNumber':" + "'" + mini.getbyName("FSearchKey").getValue() + "',";
            json = json + "'mName':" + "'" + mini.getbyName("FSearchName").getValue() + "',";
            json = json + "'mDatatype':" + "'" + mdatatype + "'";
            json = json + "}]";
            grid.load({ mydata: json});

            mExportDataUrl = grid.url;
            mExportDataJson = json;//mydata:mExportDataJson 
        }
    }

    //搜索资料列表
    function search(mdatatype) {
        LoadListData(mdatatype);
    }

    ///增加分类
    function addClass() {
        var node = tree.getSelectedNode();
        mini.open({
            url: "../GoodClass/GoodClassInfo",
            title: "商品分类", width: 670, height: 400,
            onload: function () {
                var iframe = this.getIFrameEl();
                var mClassID = node.FItemID;
                if (node.FModifyFlag != 1) mClassID = -1;
                var data = {
                    action: "new", FClassID: mClassID, FClassNumber: node.FNumber, FClassName: node.FName,
                    FIcon: node.FIcon, FCorpID: "0"
                };
                iframe.contentWindow.SetClassData(data);
            },
            ondestroy: function (action) {
                reLoadListTree();
            }
        });
    }

    //修改分类
    function editClass() {
        var node = tree.getSelectedNode();
        if (node) {
            if (node.FItemID < 0) return;
            if (node.FModifyFlag != 1) {
                mini.alert("没有权限修改该商品资料.");
                return;
            }
            mini.open({
                url: "../GoodClass/GoodClassInfo",
                title: "修改分类", width: 670, height: 400,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = {
                        action: "edit", FItemID: node.FItemID, FClassID: node.FItemID,
                        FClassNumber: node.FNumber, FClassName: node.FName, FParentID: node.FParentID
                    };
                    iframe.contentWindow.SetClassData(data);
                },
                ondestroy: function (action) {
                    if ((action) && (action != "close")) {
                        node.FName = action;
                        tree.updateNode(node);
                    }
                }
            });
        }
    }

    //删除分类
    function removeClass() {
        if (grid.getData().length > 0) {
            mini.alert("该分类有商品资料存在，不能删除！ ");
            return;
        }
        var tree = mini.get("tree1");
        var node = tree.getSelectedNode();
        if (node.FModifyFlag != 1) {
            mini.alert("没有权限删除该商品资料.");
            return;
        }
        var FSystemAppID = parent.window.getSystemAppID();
        if (node.FItemID > 0) {
            mini.confirm("确定要删除选择的记录吗？", "提示",
                function (action) {
                    if (action == "ok") {
                        var json = "{'FItemID':'" + node.FItemID + "'}";
                        $.post("../GoodClass/delGoodClass", { json: json, mSystemAppID: FSystemAppID }, function (data) {
                            var ret = mini.decode(data);
                            if (ret.status == 1) {
                                reLoadListTree();
                                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                            }
                            else {
                                mini.alert("出现错误，错误信息:" + ret.error);
                            }
                        });
                    }
                });
        }
    }

    ///增加资料
    function newGoods() {
        var uID = parent.window.getSystemAppID();
        var node = tree.getSelectedNode();
        //if (node.FItemID <= 0) {
        //    mini.alert("请选择一个商品分类再新增");
        //    return;
        //}
        if (node.FModifyFlag != 1) {
            mini.alert("不能在该商品分类下增加本店商品.");
            return;
        }
        mini.open({
            url: "/Goods/GoodsInfo",
            title: "商品资料", width: 700, height: 545,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new", FItemID: "0", FClassID: node.FItemID, FClassNumber: node.FNumber, FClassName: node.FName, FFreshType: node.FFreshType };
                iframe.contentWindow.SetInfoData(data);
            },
            ondestroy: function (action) {
                grid.reload();
            }
        });
    }

    //修改资料
    function modify() {
        if (!mini.get("edit").getVisible()) return;
        var row = grid.getSelected();
        if (row) {
            if (row.FModifyFlag != 1) {
                //mini.alert("没有权限修改该商品资料.");                
                modifyMall(row);
            }
            else {
                mini.open({
                    url: "/Goods/GoodsInfo",
                    title: "商品资料", width: 700, height: 545,
                    onload: function () {
                        var iframe = this.getIFrameEl();
                        var data = { action: "edit", FItemID: row.FItemID, FClassID: row.FClassID };
                        iframe.contentWindow.SetInfoData(data);
                    },
                    ondestroy: function (action) {
                        grid.reload();
                    }
                });
            }

        } else {
            mini.alert("请选中一条记录");
        }
    }

    //修改资料
    function modifyMall(row) {
        mini.open({
            url: "/Goods/GoodsMall",
            title: "分店商品设置", width: 700, height: 490,
            onload: function () {
                var iframe = this.getIFrameEl();
                iframe.contentWindow.SetInfoData(row);
            },
            ondestroy: function (action) {
                grid.reload();
            }
        });
    }

    //删除资料
    function removeRows() {
        var row = grid.getSelected();        
        if (row) {
            if (row.FModifyFlag != 1) {
                mini.alert("没有权限删除该商品资料.");
            }
            else {
                removegoods();
            }
        } else {
            mini.alert("请选要删除的记录");
        }
    }

    function removegoods() {
        mini.confirm("确定要删除选中的记录吗？", "提示",
            function (action) {
                if (action == "ok") {
                    SetRemoveRows();
                }
            });
    }

    function SetRemoveRows() {
        var FSystemAppID = parent.window.getSystemAppID();
        var rows = grid.getSelecteds();
        var json = "[";
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].FModifyFlag == 1) {
                if (i > 0) json = json + ',';
                js = '{"FItemID":' + rows[i].FItemID + '}';
                json = json + js
            }
        }
        json = json + "]";
        if (json.length < 8) {
            mini.alert("没有要保存的数据！");
            return;
        }
        $.post("RemoveGoodRows", { json: json, mSystemAppID: FSystemAppID }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                grid.reload();
            }
            else {
                mini.alert("异常:" + ret.error);
            }
        });
    }

    function setkeyPressCls() {
        if (event.keyCode == 13) {
            findtree();
        }
    }

    function findtree() {
        var key = mini.get("TreeKey").getValue();
        if (key == "") {
            tree.clearFilter();
        } else {
            key = key.toLowerCase();
            tree.filter(function (node) {
                var text = node.FName ? node.FName.toLowerCase() : "";
                if (text.indexOf(key) != -1) {
                    return true;
                }
            });
        }
    }

    function ExcelImport() {
        //var FSystemAppID = parent.window.getSystemAppID();
        //var mjson = mini.getbyName("FSearchKey").getValue();
        //$.post("GetImportState", { json: mjson, mSystemAppID: FSystemAppID }, function (data) {
        //    var ret = mini.decode(data);
        //    if (ret.status == 1) {
        //        parent.window.showTabById('GoodsUpload', '导入商品资料', '../Goods/GoodsUpload');
        //    }
        //    else {
        //        mini.alert(ret.msg);
        //    }
        //});

        
        parent.window.showTabById('GoodsUpload', '导入商品资料', '../Goods/GoodsUpload');

        //var FSystemAppID = parent.window.getSystemAppID();
        //mini.open({
        //    url: "../data/Import.aspx?mDataType=1&mSystemAppID=" + FSystemAppID,
        //    title: "商品Excel导入", width: 420, height: 300
        //});
    }

    function CopyFrom() {
        parent.window.showTabById('Goods', '导入商品资料', '../Goods/GetShareGoods');
    }

    function saveas() {
        var grid = mini.get("datagrid1");
        var rows = grid.getChanges();
        var json = mini.encode(rows);
        $.post("saveas", { json: json, mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: '保存成功', state: "success", x: "center", y: "top", timeout: 3000 });
            }
            else {
                mini.showTips({ content: '保存失败 ' + ret.error, state: "danger", x: "center", y: "top", timeout: 3000 });
            }
        });
    }

    function ImportGoods() {
        parent.window.showTabById('ImportGoods', '引入商品', '../Goods/ImportGoods');
    }

    function setMallFlag() {
        var grid = mini.get("datagrid1");
        var rows = grid.getSelecteds();
        if (rows.length == 0) {
            mini.alert("请选择商品进行上架");
        }
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            grid.updateRow(row, { FMallFlag: 1 });
        }
        saveas();
    }

    function setRequire() {
        var grid = mini.get("datagrid1");
        var rows = grid.getSelecteds();
        if (rows.length == 0) {
            mini.alert("请选择商品进行要货");
        }
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            grid.updateRow(row, { FRequireFlag: 1 });
        }
        saveas();
    }

    function setMallPrice() {
        mini.confirm("确定要用总店的商品电商价覆盖本店的商品电商价吗？", "提示", function (action) {
            if (action == "ok") {
                setMallPriceExe();
            }
        });
    }

    function setMallPriceExe() {
        var json = '[{"rows":"' + "0" + '","project":' + '10' + ',';
        json += '"value":"1",'
        json += '"mSystemAppID":"' + parent.window.getSystemAppID() + '"}]';
        $.post('../Goods/SaveBatch', { mjson: json }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 }, function () {
                    mini.alert("同步完成。");
                });
            } else {
                mini.alert("保存失败，" + ret.error);
            }
        });
    }

    function Batchset() {
        var rows = grid.getSelecteds();
        var data = '';
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].FModifyFlag == 1) {
                if ((i > 0) && (data.length > 0)) data += ',';
                data += rows[i].FItemID;
            }
        }
        if ((rows.length > 0) && (data.length > 0)) {
            mini.open({
                url: "/Goods/BatchSet",
                title: "批量设置", width: 700, height: 470,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    iframe.contentWindow.SetInfoData(data);
                },
                ondestroy: function (action) {
                    grid.reload();
                }
            });
        }
        else {
            mini.alert("请选择商品进行批量设置");
        }
    }

    function loaded(e) {
        if (e.data.length == 0) {
            mini.getbyName("export").set({ visible: false });
            //mini.getbyName("import").set({ visible: false });
            return;
        }
        if (e.data[0].FExport == 1) {
            mini.getbyName("export").set({ visible: true });
            //mini.getbyName("import").set({ visible: true });
        }
        else {
            mini.getbyName("export").set({ visible: false });
            //mini.getbyName("import").set({ visible: false });
        }
    }
    
</script>